package com.feng.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.reggie.common.R;
import com.feng.reggie.entity.ShoppingCart;
import com.feng.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车数据
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userid = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userid);
        //查询菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(ShoppingCart::getUserId,userid);
        if(dishId!=null){
            //添加到购物车的是菜品
         queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one!=null){
            //如果已经在的话，数量加1
            one.setNumber(one.getNumber()+1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one);
        }
        //如果不存在，则添加到购物车，数量默认为1
        else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;

        }
        return R.success(one);
    }

    /**
     * 查看购物车数据
     * @param session
     * @return
     */
   @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        log.info("查看购物车....");
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
       Long user = (Long) session.getAttribute("user");
       queryWrapper.eq(ShoppingCart::getUserId,user);
       queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
       List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
       return R.success(list);
   }

    /**
     * 减少菜品或套餐
     * @param shoppingCart
     * @param session
     * @return
     */
   @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
       //查看用户减少的是菜品还是套餐
       Long setmealId = shoppingCart.getSetmealId();
       Long dishId = shoppingCart.getDishId();

       //获得当前登录用户的id
       Long userid = (Long) session.getAttribute("user");
       LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
       //添加查询条件
       queryWrapper.eq(ShoppingCart::getUserId,userid);

       //判断是否减少的是菜品
       if(dishId!=null){
           queryWrapper.eq(ShoppingCart::getDishId,dishId);
       }
       else {
           //减少的是套餐id
           queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
       }
       ShoppingCart one = shoppingCartService.getOne(queryWrapper);

       //判断数量是否是1，如果是1，直接删除即可
       if(one.getNumber()==1){
           shoppingCartService.remove(queryWrapper);
       }
       else {
           one.setNumber(one.getNumber()-1);
           one.setCreateTime(LocalDateTime.now());
           shoppingCartService.updateById(one);

       }
       return  R.success(one);


    }

    /**
     * 清空购物车数据
     * @param session
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long userid = (Long) session.getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功！");
    }

}
